

var liujing={};

function debug(a){
	//var win=window.open("","_blank");
	//win.document.write(a);
	//return a;
	alert(a);
}

liujing.Label=function(/*component/html/object*/ obj){	
	if(Ext.type(obj)=='string'){
		this.setHTML(obj);
	}else{
		this.setContent(obj);
	}
	Ext.Component.prototype.constructor.call(this,{autoEl:'div'});
	this.cls="lj-label";
	//this.style="border:2px solid green";
};

Ext.extend(liujing.Label,Ext.BoxComponent,{
		_text:'',
		_html:'',
		_contentComponent:null,
	onRender:function(ct, position){
		//liujing.Label.superclass.onRender.call(this,ct, position);
		Ext.BoxComponent.prototype.onRender.call(this,ct, position);
		this.drawContent();
	},
	setText:function(strText){
		this._text=strText;
		if(this.rendered){
			this.drawContent();
		}
	},
	setHTML:function(strText){
		this._html=strText;
		if(this.rendered){
			this.drawContent();
		}
	},
	drawContent:function(){
		if(this._contentComponent!=null){
			this._contentComponent.render(this.getEl().dom);
		}else if(this._html){
			this.getEl().dom.innerHTML=this._html;
		}else if(this._text){			
			this.getEl().dom.appendChild(document.createTextNode(this._text));
		}
	},
	setContent:function(component){
		if(this._contentComponent!=null){
			this._contentComponent.destory();
		}
		this._contentComponent=component;
		if(this.rendered){
			this.drawContent();
		}
	},
	onDestory:function(){
		liujing.Label.superclass.onDestory.call(this);
		if(this._contentComponent){
			this._contentComponent.destory();
		}
		this.getEl().dom.innerHTML="";
	}
});

liujing.Glue=function(){
	liujing.Glue.superclass.constructor.apply(this,arguments);
};
Ext.extend(liujing.Glue,liujing.Label);



liujing.Panel=function(config){
	if(!Ext.type(config)){
		config={};
	}
	if(!Ext.type(config.autoEl))
		config.autoEl='div';
	if(!Ext.type(config.layout))
		config.layout="ljcolumn";
	
	liujing.Panel.superclass.constructor.call(this,config);
	//this.style="border:1px solid red";
};
Ext.extend(liujing.Panel,Ext.Container,
{
	//getResizeEl : function(){
	//	return this.wrap;
	//},
	//
	//    
	//getPositionEl : function(){
	//	return this.wrap;
	//},
	render:function(){
		liujing.Panel.superclass.render.apply(this,arguments);
	},
	onRender:function(ct, position){
		
		liujing.Panel.superclass.onRender.call(this,ct,position);
		if(this.align){
			this.getEl().dom.align=this.align;
		}
		
		if(this.ljBorder && (this.height<this.ljBorder.minHeight || typeof(this.height)!="number"))
			this.setHeight(this.ljBorder.minHeight);
	},
	afterRender:function(){
		
		liujing.Panel.superclass.afterRender.apply(this,arguments);
		
		if(this.ljBorder){
			this.positionEl=this.getEl();
			var oldEl=this.getEl();
			this.ljBorder.renderToDom(oldEl.dom);
			this.el=Ext.get(this.ljBorder.contentDom);
			this.resizeEl=oldEl;
			this.positionEl=oldEl;
		}
	},
	onResize:function(adjWidth, adjHeight, rawWidth, rawHeight){
		if(this.ljBorder){
			
			this.ljBorder.onResize();
		}
	}
	,setLjBorder:function(border){
		this.ljBorder=border;
	}
	
});


liujing.Border2=function(title){
	var imgDir=liujing.ROOT_DIR+'/js/img/ui/window/simple1/';
	this.borderData=[
		{w:17,h:17,url:imgDir+'BorderTopLeft.png'},
		{h:17,url:imgDir+'BorderTop.png'},
		{w:23,h:17,url:imgDir+'BorderTopRight.png'},
		{w:17,url:imgDir+'BorderLeft.png'},
			//tl:{w:'17',h:'17',url:imgDir+'BorderTopLeft.png'},
		{w:23,url:imgDir+'BorderRight.png'},
		{w:17,h:23,url:imgDir+'BorderBottomLeft.png'},
		{h:23,url:imgDir+'BorderBottom.png'},
		{w:23,h:23,url:imgDir+'BorderBottomRight.png'}
	];
	this.contentDomStyle={top:8,left:8,right:14,bottom:14,imgUrl:liujing.ROOT_DIR+"/js/img/ui/tran-red.png"};
	this.titleDomStyle={h:23,imgUrl:imgDir+"Header.png",color:"#e0e0e0",paddingLeft:"10"};
	this.setTitle(title);
	this.minHeight=this.borderData[0].h+this.borderData[5].h+10;
};
liujing.Border2.prototype={
	rendered:false,
	containerEl:null,
	paintBorder:function(width,height,containerDom){
		containerDom.style.overflow="hidden";
		//for IE, must set this, otherwise we can't resize it to a smaller size,
		//if it has already contained some children doms
		var containerEl=Ext.get(containerDom);
		this.containerEl=containerEl;
		width=containerEl.getWidth();
		height=containerEl.getHeight();
		
		var root=document.createElement('div');
		//root.style.border="1px solid yellow";
		//root.style.width=width+'px';
		//root.style.height=height+'px';
		root.style.position="relative";
		containerDom.appendChild(root);
		
		var borderDoms=[];
		for(var i=0;i<8;i++){
			var cell=document.createElement('div');
			borderDoms.push(cell);
			cell.style.position="absolute";
			cell.style.overflow="hidden";//for IE6 must add, otherwise DIV will have a minimum height 19px
			
			var d=this.borderData[i];
			//if(i>=0 && i<3){
			//	cell.style.top='0px';
			//}
			//if(i>=5 && i<8){
			//	cell.style.top=height-this.borderData[5].h+'px';
			//}
			//if(typeof(d.w)!='undefined'){
			//	cell.style.width=d.w+'px';
			//}
			//if(typeof(d.h)!='undefined'){
			//	cell.style.height=d.h+'px';
			//}
			if(Ext.isIE6){
				cell.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale, src='"+
					d.url+"')";
			}
			else{
				cell.style.background="url("+d.url+")";
			}
			
			root.appendChild(cell);
		}
		//borderDoms[0].style.left="0px";
		//borderDoms[1].style.left=this.borderData[0].w+"px";
		//borderDoms[1].style.width=width-this.borderData[0].w-this.borderData[2].w+"px";
		//borderDoms[2].style.left=width-this.borderData[2].w+"px";
		//
		//borderDoms[3].style.left="0px";
		//borderDoms[3].style.top=this.borderData[0].h+"px";
		//borderDoms[3].style.height=height-this.borderData[0].h-this.borderData[5].h+"px";
		//borderDoms[4].style.left=width-this.borderData[4].w+"px";
		//borderDoms[4].style.top=this.borderData[2].h+"px";
		//borderDoms[4].style.height=height-this.borderData[2].h-this.borderData[7].h+"px";
		//
		//borderDoms[5].style.left="0px";
		//borderDoms[6].style.left=this.borderData[5].w+"px";		
		//borderDoms[6].style.width=width-this.borderData[5].w-this.borderData[7].w+"px";
		//borderDoms[7].style.left=width-this.borderData[7].w+"px";
		this.wrapDom=root;
		
		this.contentDom=document.createElement('div');
		this.contentDom.style.position="absolute";
		for(var name in this.contentDomStyle){
			if(typeof(this.contentDomStyle[name])!='number'){
				this.contentDom.style[name]=this.contentDomStyle[name];
			}
		}
		if(Ext.isIE6){
			this.contentDom.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale, src='"+
				this.contentDomStyle.imgUrl+"')";
		}
		else{
			this.contentDom.style.background="url("+this.contentDomStyle.imgUrl+")";
		}
		//this.contentDom.style.left=this.contentDomStyle.left+"px";
		//this.contentDom.style.top=this.contentDomStyle.top+"px";
		//this.contentDom.style.width=width-this.contentDomStyle.left-this.contentDomStyle.right+"px";
		//this.contentDom.style.height=height-this.contentDomStyle.top-this.contentDomStyle.bottom+"px";
		root.appendChild(this.contentDom);
		
		if(this.title){
			this.titleDom=document.createElement('div');
			tst=this.titleDom.style;
			tst.position='relative';
			tst.overflow="hidden";
			tst.height=this.titleDomStyle.h+'px';
			for(var name in this.titleDomStyle){
				if(typeof(this.titleDomStyle[name])!='number'){
					tst[name]=this.titleDomStyle[name];
				}
			}
			this.titleDom.appendChild(document.createTextNode(this.title));	
			this.contentDom.appendChild(this.titleDom);
		}
		this.borderDoms=borderDoms;
		this.rendered=true;
		this.setSize(width,height);
	},
	setSize:function(width,height){
		if(height<this.minHeight || width<=0){
			alert("invalid width or height:"+width+", "+height);
		}
		this.wrapDom.style.width=width+'px';
		this.wrapDom.style.height=height+'px';
		
		var borderDoms=this.borderDoms;
		for(var i=0;i<8;i++){
			var cell=this.borderDoms[i];
			var d=this.borderData[i];
			if(i>=0 && i<3){
				cell.style.top='0px';
			}
			if(i>=5 && i<8){
				cell.style.top=height-this.borderData[5].h+'px';
			}
			if(typeof(d.w)!='undefined'){
				cell.style.width=d.w+'px';
			}
			if(typeof(d.h)!='undefined'){
				cell.style.height=d.h+'px';
			}
		}
		borderDoms[0].style.left="0px";
		borderDoms[1].style.left=this.borderData[0].w+"px";
		borderDoms[1].style.width=width-this.borderData[0].w-this.borderData[2].w+"px";
		borderDoms[2].style.left=width-this.borderData[2].w+"px";
		
		borderDoms[3].style.left="0px";
		borderDoms[3].style.top=this.borderData[0].h+"px";
		borderDoms[3].style.height=height-this.borderData[0].h-this.borderData[5].h+"px";
		borderDoms[4].style.left=width-this.borderData[4].w+"px";
		borderDoms[4].style.top=this.borderData[2].h+"px";
		borderDoms[4].style.height=height-this.borderData[2].h-this.borderData[7].h+"px";
		
		borderDoms[5].style.left="0px";
		borderDoms[6].style.left=this.borderData[5].w+"px";		
		borderDoms[6].style.width=width-this.borderData[5].w-this.borderData[7].w+"px";
		borderDoms[7].style.left=width-this.borderData[7].w+"px";
		this.contentDom.style.left=this.contentDomStyle.left+"px";
		this.contentDom.style.top=this.contentDomStyle.top+"px";
		this.contentDom.style.width=width-this.contentDomStyle.left-this.contentDomStyle.right+"px";
		this.contentDom.style.height=height-this.contentDomStyle.top-this.contentDomStyle.bottom+"px";
		
	},
	onResize:function(){
		if(this.rendered){
			var width=this.containerEl.getWidth();
			var height=this.containerEl.getHeight();
			this.setSize(width,height);
		}
	}
	,setTitle:function(sText){
		this.title=sText;
	}
	,getWrapDom:function(){
		return this.wrapDom;
	}
	,renderToDom:function(containerDom){
		
		var cEl=Ext.get(containerDom);		
		this.paintBorder(cEl.getWidth(),cEl.getHeight(),containerDom);
	}
};
liujing.BaseLayout=function(){
	liujing.BaseLayout.superclass.constructor.apply(this,arguments);
};
Ext.extend(liujing.BaseLayout,Ext.layout.ContainerLayout,
{
	monitorResize:true,
	onResize:function(){
		liujing.BaseLayout.superclass.onResize.apply(this,arguments);
	},
	setContainer:function(ct){
		liujing.BaseLayout.superclass.setContainer.apply(this,arguments);
	}
});
Ext.Container.LAYOUTS['ljbase'] = liujing.BaseLayout;

liujing.ColumnLayout=function(strAxis){
	liujing.ColumnLayout.superclass.constructor.call(this,{});
};
Ext.extend(liujing.ColumnLayout,liujing.BaseLayout,
{
	renderAll:function(ct, target){
		if(target.dom.style.position!="absolute")
			target.dom.style.position="relative";
		target.dom.style.overflow="hidden";
		var items = ct.items.items;
		var width=target.getWidth();
		var height=target.getHeight();
		var leftOffset=0;
		var columnWidths=[];
		var restWidth=width;
		var computWidthNum=0;
		var positionEls=[];
		/*for(var i = 0, len = items.length; i < len; i++){
			if(items[i].constructor==liujing.Glue){
			}
		}*/
		for(var i = 0, len = items.length; i < len; i++) {
		    var c = items[i];
		    if(typeof(c.setPosition)!="function"){
			//not a Boxcomponent
			
			var realWidth=this.fixedRenderUnbox(c,target,width,height,positionEls);
			columnWidths.push(realWidth);
			//console.debug(" w="+realWidth);
		    }else if((typeof(c.width)=="number" && !c.rendered) || c.forceWidth){
			    columnWidths.push(c.width);
			    c.forceWidth=true;
		    }else{
			    c.isLayoutWidth=true;
			    columnWidths.push(0);
			    computWidthNum++;
		    }
		    restWidth-=columnWidths[i];
		}
		if(restWidth>0 && computWidthNum>0){
			var fixWidth=Math.floor(restWidth/computWidthNum);
			for(var i=0;i<columnWidths.length;i++){
				if(columnWidths[i]==0)
					columnWidths[i]=fixWidth;
			}
		}
		
		var len=items.length;
		var lastIdx=len-1;
		var positionIdx=0;
		for(var i = 0, len = items.length; i < len; i++) {
			var c = items[i];
			var isBoxComponent= typeof(c.setPosition)=="function";
			if(positionIdx<positionEls.length){
				//console.debug("insert id "+c.id+" before "+positionIdx);
				leftOffset+=this.fixedRender(c,target,leftOffset,width,height,columnWidths[i],isBoxComponent,positionEls[positionIdx]);
			}else{
				leftOffset+=this.fixedRender(c,target,leftOffset,width,height,columnWidths[i],isBoxComponent);
			}
			if(!isBoxComponent)
				positionIdx++;
		}
		//target.dom.style.border="1px solid red";
	},
	adjustCompAlign:function(c,width,height,isBoxComponent,leftOffset){
		var t = c.getPositionEl ? c.getPositionEl() : c.getEl();
		t.dom.style.position="absolute";
		t.dom.style.margin="0px";
		if(typeof(c.alignmentY)=="undefined"){
			c.alignmentY=0.5;
		}
		if(c.alignmentY>0){
			var compHeight=t.getHeight();
			var tempY=height-compHeight;
			if(tempY>0){
				if(isBoxComponent){
					c.setPosition(leftOffset,tempY*c.alignmentY);
				}else{
					t.dom.style.top=tempY*c.alignmentY+"px";
					t.dom.style.left=leftOffset+"px";
				}
			}
		}
		return t.getWidth();
	},
	fixedRenderUnbox:function(c,target,width,height,positionEls){
		this.renderItem(c,target);
		positionEls.push(c.getEl());
		var realWidth=this.adjustCompAlign(c,width,height,false,0);
		return realWidth;
	},
	fixedRender:function(c,target,leftOffset,width,height,columnWidth,isBoxComponent,position){
		var realWidth;
		if(isBoxComponent){
			c.setPosition(leftOffset,0);
			if((c.height && !c.rendered)||c.forceHight){
			   c.setSize(columnWidth,c.height);
			   c.forceHight=true;
			}else if(c instanceof Ext.Container){
				c.setSize(columnWidth,height);
			}else{
				c.setWidth(columnWidth);
				//c.setSize(columnWidth,height);
			}
			this.renderItem(c, target, position);
			realWidth=this.adjustCompAlign(c,width,height,isBoxComponent,leftOffset);
		}else{
			c.getEl().dom.style.left=leftOffset+"px";
			realWidth=columnWidth;
		}
		return realWidth;
	},
	renderItem: function(c,target,position){
        if(c && !c.rendered){
        	c.render(target,position);
        	if(this.extraCls){
        		var t = c.getPositionEl ? c.getPositionEl() : c;
        		t.addClass(this.extraCls);
        	}
        	if (this.renderHidden && c != this.activeItem) {
        	    c.hide();
        	}
	}
    },
	isValidParent:function(c, target){
		return true;
	}
 });

Ext.Container.LAYOUTS['ljcolumn'] = liujing.ColumnLayout;
Ext.Container.LAYOUTS['lj-x'] = liujing.ColumnLayout;

liujing.RowLayout=function(){
	liujing.RowLayout.superclass.constructor.apply(this.arguments);
};
Ext.extend(liujing.RowLayout,liujing.ColumnLayout,
{
	renderAll:function(ct, target){
		if(target.dom.style.position!="absolute")
			target.dom.style.position="relative";
		target.dom.style.overflow="hidden";
		var items = ct.items.items;
		var width=target.getWidth();
		var height=target.getHeight();
		var topOffset=0;
		var rowHeights=[];
		var restHeight=height;
		var computWidthNum=0;
		var positionEls=[];
		for(var i = 0, len = items.length; i < len; i++) {
		    var c = items[i];
		    if(typeof(c.setPosition)!="function"){
			//not a Boxcomponent			
			var realHeight=this.fixedRenderUnbox(c,target,width,height,positionEls);
			rowHeights.push(realHeight);
		    }else if((typeof(c.height)=="number" && !c.rendered) || c.forceHeight){
			    rowHeights.push(c.height);
			    c.forceHeight=true;
		    }else{
			    c.isLayoutHeight=true;
			    rowHeights.push(0);
			    computWidthNum++;
		    }
		    restHeight-=rowHeights[i];
		}
		if(restHeight>0 && computWidthNum>0){
			var fixHeight=Math.floor(restHeight/computWidthNum);
			for(var i=0;i<rowHeights.length;i++){
				if(rowHeights[i]==0)
					rowHeights[i]=fixHeight;
			}
		}
		
		var len=items.length;
		var lastIdx=len-1;
		var positionIdx=0;
		for(var i = 0, len = items.length; i < len; i++) {
			var c = items[i];
			var isBoxComponent= typeof(c.setPosition)=="function";
			if(positionIdx<positionEls.length){
				//console.debug("insert id "+c.id+" before "+positionIdx);
				topOffset+=this.fixedRender(c,target,topOffset,width,height,rowHeights[i],isBoxComponent,positionEls[positionIdx]);
			}else{
				topOffset+=this.fixedRender(c,target,topOffset,width,height,rowHeights[i],isBoxComponent);
			}
			if(!isBoxComponent)
				positionIdx++;
		}
		//target.dom.style.border="1px solid red";
	},
	adjustCompAlign:function(c,width,height,isBoxComponent,topOffset){
		var t = c.getPositionEl ? c.getPositionEl() : c.getEl();
		t.dom.style.position="absolute";
		t.dom.style.margin="0px";
		if(typeof(c.alignmentX)=="undefined"){
			c.alignmentX=0.5;
		}
		if(c.alignmentX>0){
			var compW=t.getWidth();
			var tempX=width-compW;
			if(tempX>0){
				if(isBoxComponent){
					c.setPosition(tempX*c.alignmentX,topOffset);
				}else{
					t.dom.style.left=tempX*c.alignmentX+"px";
					t.dom.style.top=topOffset+"px";
				}
			}
		}
		return t.getHeight();
	},
	fixedRenderUnbox:function(c,target,width,height,positionEls){
		this.renderItem(c,target);
		positionEls.push(c.getEl());
		var real=this.adjustCompAlign(c,width,height,false,0);
		return real;
	},
	fixedRender:function(c,target,topOffset,width,height,rowHeight,isBoxComponent,position){
		var realHeight;
		if(isBoxComponent){
			c.setPosition(0,topOffset);
			if((c.width && !c.rendered)||c.forceWidth){
			   c.setSize(c.width,rowHeight);
			   c.forceWidth=true;
			}else if(c instanceof Ext.Container){
				c.setSize(width,rowHeight);
			}else{
				c.setHeight(rowHeight);
			}
			this.renderItem(c, target, position);
			realHeight=this.adjustCompAlign(c,width,height,isBoxComponent,topOffset);
		}else{
			c.getEl().dom.style.top=topOffset+"px";
			realHeight=rowHeight;
		}
		return realHeight;
	}
});
Ext.Container.LAYOUTS['lj-y'] = liujing.RowLayout;	
liujing.TableLayout=function(){
	liujing.TableLayout.superclass.constructor.apply(this,arguments);
};

Ext.extend(liujing.TableLayout,Ext.layout.TableLayout,
{
	getNextCell:function(c){
		var td=liujing.TableLayout.superclass.getNextCell.call(this,c);
		if(c.vAlign){
			td.vAlign=c.vAlign;
		}
		if(c.align){
			td.align=c.align;
		}
		return td;
	}
});
Ext.Container.LAYOUTS['lj-table'] = liujing.TableLayout;
